<?php
/**
 * 节点规则管理
 */
namespace app\admin\controller;
use app\admin\model\AuthGroup;
use app\admin\model\AuthRule;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Rule extends Base
{
    /**
     * 规则列表
     * @return mixed|string
     * @throws \Exception
     */
    public function index(){
        if (Request::isAjax()) {
            $data=input('param.');
            if(isset($data['pid'])&&$data['pid']!=''){
                //此处不需要任何操作
            }else{
                $data['pid']=0;//默认菜单起点为0
            }
            $ruleModel = new AuthRule();
            return $ruleModel->tableData($data);
        }else{
            return View::fetch('index');
        }

    }
    /**
     * 规则添加
     * @return array|mixed
     */
    public function add()
    {
        $result = [
            'status' => false,
            'msg' => '失败',
            'data' => ''
        ];
        $rule_model = new AuthRule();
        if(Request::isPost())
        {
            $data = Request::post();
            $rule_model ->save($data);
            $result['status'] = true;
            $result['msg'] = '添加成功';
            return $result;
        }
        $list = $rule_model->select();
        $list = tree($list);
        $pid = Request::param('id') ? Request::param('id') : 0;
        $view =[
            'info'     => null,
            'pid'      => $pid,
            'ruleList' => $list
        ];
        //halt($view);
        View::assign($view);
        $return['status'] = true;
        $return['msg'] = '成功';
        $return['data'] = View::fetch();
        return $return;

    }
    /**
     *  子节点
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addSon()
    {
        $ruleModel = new AuthRule();
        $ruleInfo = $ruleModel->where('id', input('param.id/d'))->find();
        if (!$ruleInfo) return error_code(10002);

        return [
            'status' => true,
            'msg'    => '获取成功',
            'data'   => View::fetch('addson', ['ruleInfo' => $ruleInfo])
        ];
    }
    /**
     * 节点修改
     * @return array|mixed
     */
    public function edit()
    {
        $result = [
            'status' => false,
            'msg' => '失败',
            'data' => ''
        ];
        $rule_model = new AuthRule();
        if(Request::isPost())
        {

            $data = Request::post();
            $where['id'] = $data['id'];
            AuthRule::update($data, $where);
//            $rule_model ->save($data);
            $result['status'] = true;
            $result['msg'] = '修改成功';
            return $result;
        }
        $list = $rule_model->select();
        $list = tree($list);
        //$pid = Request::param('id') ? Request::param('id') : 0;
        $info = $rule_model->where('id', input('param.id/d'))->find();
        if (!$info) return error_code(10002);
        $view =[
            'info'     => $info,
            'pid'      => 0,
            'ruleList' => $list
        ];
        //halt($view);
        View::assign($view);
        $return['status'] = true;
        $return['msg'] = '成功';
        $return['data'] = View::fetch();
        return $return;

    }


    public function getTree(){
        $rules = Db::name('auth_group')
            ->where('id', Request::param('id'))
            ->value('rules');
        $data2['MenuIDarray']=explode(',', $rules);

        $auth_rule=new AuthRule();
        $data1=$auth_rule->treeData();

        $view = [
            'data1' => $data1,
            'data2'=>$data2
        ];
        //halt($data2);
        View::assign($view);
    }


    /**
     * 删除菜单节点，如某个节点下有子节点，则无法删除
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del()
    {
        $ruleModel = new AuthRule();
        $result           = ['status' => true, 'msg' => '删除成功', 'data' => ''];
        //判断该分类下是否有子分类
        if ($ruleModel->where('pid', input('param.id/d'))->find()) {
            $result['status'] = false;
            $result['msg']    = '该节点下有子节点无法删除';
        } else {
            if (!$ruleModel->where('id', input('param.id/d'))->delete()) {
                $result['status'] = false;
                $result['msg']    = '删除失败';
            }
        }
        return $result;
    }




}
